<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <link rel="stylesheet" href="lib/qunit/qunit.css" type="text/css" media="screen">
	<script type="text/javascript" src="lib/jquery/jquery-1.8.2.js"></script>
    <script type="text/javascript" src="lib/qunit/qunit.js"></script>
    <script type="application/x-javascript" src="lib/mobiscroll/js/mobiscroll-2.0.3.full.min.js"></script>

    <script type="text/javascript" src="datediff.js"></script>

    <script type="text/javascript">
    	
function equalDiff(comp, d, m, y, td) {
    equals(comp.getYears(), y, "Years of " + comp.getYears() + " should be " + y);
    equals(comp.getMonths(), m, "Months of " + comp.getMonths() + " should be " + m);
    equals(comp.getDays(), d, "Days of " + comp.getDays() + " should be " + d);
    equals(comp.getTotalDays(), td, "Total days of " + comp.getTotalDays() + " should be " + td);
}

function equalIndicator(comp, ind) {
	equals(comp.getIndicator(), ind);
}

function todayFullDate() {
    return new Date(Date.UTC(2012, 9 /* i.e. 10, Oct */, 23, 0, 0, 0));
}

function runTests()
{
    module("datediff");
    
    test("today looks like a date string", function() {
        var comp = new DiffComputer("", true, "", true);
		var today = comp._getToday(); // 22/10/2012
		equals(today.length, 10, "Today has a 10 chars");
		var dateR = new RegExp("^(\\d{2})/(\\d{2})/(\\d{4})$");
		ok(dateR.test(today), "Date '" + today + "' matches the date regex");
    });  

    test("dateStrToDate conversion gives a date with no time", function() {
        var comp = new DiffComputer("", true, "", true);
        var todayDate = comp._dateStrToDate("22/10/2012");
		var todayDateStr = todayDate.toUTCString();
        var dateR = new RegExp("^Mon, 22 Oct 2012 00:00:00 \\S+$");
        ok(dateR.test(todayDateStr), "Date '" + todayDateStr + "' matches the date regex with empty time");
    });  

    test("dateStrToComponents conversion gives the right date", function() {
        var comp = new DiffComputer("", true, "", true);
        var todayDateComponents = comp._dateStrToComponents("22/10/2012");
		equals(todayDateComponents[0], 22);
		equals(todayDateComponents[1], 10);
		equals(todayDateComponents[2], 2012);
    });  

    test("can handle dates before the epoch", function() {
        var comp = new DiffComputer("", true, "", true);
        var earlyDate = comp._dateStrToDate("22/02/1969");
        var earlyDateStr = earlyDate.toUTCString();
        var dateR = new RegExp("^Sat, 22 Feb 1969 00:00:00 \\S+$");
        ok(dateR.test(earlyDateStr), "Date before epoch '" + earlyDateStr + "' matches the date regex with empty time");
    });  

    test("ms before the epoch", function() {
        var comp = new DiffComputer("31/12/1969", false, "", true);
        equals(comp.dateAInDaysSinceEpoch, -1);
    });  

    test("days in month", function() {
        var comp = new DiffComputer("", true, "", true);
        equals(comp._daysInMonth(7, 2009), 31);
		equals(comp._daysInMonth(2, 2009), 28);
		equals(comp._daysInMonth(2, 2008), 29);
		equals(comp._daysInMonth(2, 2000), 29);
        equals(comp._daysInMonth(2, 2100), 28);
    });  

    test("both locked, no difference", function() {
		var comp = new DiffComputer("", true, "", true);
        equalDiff(comp, 0, 0, 0, 0);
    });  

    test("neither locked, one day difference A>B", function() {
        var comp = new DiffComputer("22/10/2012", false, "21/10/2012", false);
		equalDiff(comp, 1, 0, 0, 1);
    });  

    test("neither locked, one day difference A<B", function() {
        var comp = new DiffComputer("21/10/2012", false, "22/10/2012", false);
        equalDiff(comp, 1, 0, 0, 1);
    });  

    test("Sample dates", function() {
		var mjg = new DiffComputer("22/02/1969", false, "23/10/2012", false, todayFullDate());
        equalDiff(mjg, 1, 8, 43, 15949);
        equalDiff(new DiffComputer("13/11/2000", false, "23/10/2012", false), 10, 11, 11, 4362);
        equalDiff(new DiffComputer("04/11/1928", false, "17/04/2000", false), 13, 5, 71, 26097);
		// today is injected as 23/10/2012
		equalDiff(new DiffComputer("02/04/2008", false, "", true, todayFullDate()), 21, 6, 4, 1665);
        var xmas = new DiffComputer("", true, "25/12/2012", false, todayFullDate());
        equalDiff(xmas, 2, 2, 0, 63);
    });  

    test("indicators", function() { // today is injected as 23/10/2012
        equalIndicator(new DiffComputer("13/11/2000", false, "20/10/2012", false, todayFullDate()), '<'); // both <
        equalIndicator(new DiffComputer("04/11/1928", false, "23/10/2012", false, todayFullDate()), '<'); // both <=
        equalIndicator(new DiffComputer("23/10/2012", false, "23/10/2012", false, todayFullDate()), '-'); // both =
        equalIndicator(new DiffComputer("20/10/2012", false, "28/10/2012", false, todayFullDate()), '-'); // spanning
		equalIndicator(new DiffComputer("23/10/2012", false, "26/10/2012", false, todayFullDate()), '>'); // both =>
		equalIndicator(new DiffComputer("24/10/2012", false, "28/10/2012", false, todayFullDate()), '>'); // both >
    });  

}
    </script>
</head>
<body onload="runTests()">
 <h1 id="qunit-header">DateDiff QUnit Tests</h1>
 <h2 id="qunit-banner"></h2>
 <div id="qunit-testrunner-toolbar"></div>
 <h2 id="qunit-userAgent"></h2>
 <ol id="qunit-tests"></ol>
</body>
</html>
